﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CMISLibrary.Libraries;
using CMISLibrary;
using System.Data;
using System.Threading;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using Microsoft.Windows.Controls;
using bus_TienichNPC;
using BCNPCEntities;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for THONGTIN_KHANG.xaml
    /// </summary>
    public partial class THONGTIN_KHANG : UserControl
    {
        #region Khai báo biến

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        string strMasogcs = "";
        string strMatram = "";
        string strErr = "";
        string strMA_KHANG = "";
        string strMA_DDO = "";
        string strDienthoai = "";
        string strEmail = "";
        string strDuongPho = "";
        string strDiachiDDO = "";
        string strReKH = "";
        string strReDDO = "";
        
        Int32 IDVitriDDO = 0;
        Int64 id_vitri_ddo = 0;

        bool checkchungDiachiKH = false;
        bool checkchungDiachiDDO = false;
        bool checkReAll = false;
        
        DataSet dsAll = new DataSet();
        DataTable dtDDO = new DataTable();
        DataTable dtVITRI = new DataTable();
        DataTable dtKHANG = new DataTable();

        DataSet DsTableInsert = new BCNPCEntities.EXCEL.DstableExcel();
        DataSet DsTableDelete = new BCNPCEntities.EXCEL.dstableDelete();

        cls_TienichNPC_bus cls = new cls_TienichNPC_bus();
        cls_THONGTIN_KHANG clsTTKHANG = new cls_THONGTIN_KHANG();

        #endregion


        public THONGTIN_KHANG()
        {
            InitializeComponent();
            Component.TabIndexManager.RegisterTabManager(this);

            rdbSogcs.IsChecked = true;
            //ckbDiachiKH.Checked = false;
            txtDiachiKH.IsEnabled = false;
            txtDiachiDDO.IsEnabled = false;
            txtReKH.IsEnabled = false;
            txtReDDO.IsEnabled = false;

            checkchungDiachiKH = false;
            checkchungDiachiDDO = false;
            checkReAll = true;            
        }

        protected void del_data_Inserted()
        {
            bool dl_HDG_VITRI_DDO = GlobalServices.Service_NPCUS.delete_HDG_VITRI_DDO_TDTT(DsTableDelete, ref strErr);
            bool dl_HDG_DIEM_DO = GlobalServices.Service_NPCUS.delete_HDG_DIEM_DO_TDTT(DsTableDelete, ref strErr);
            bool dl_HDG_KHACH_HANG = GlobalServices.Service_NPCUS.delete_HDG_KHACH_HANG_TDTT(DsTableDelete, ref strErr);    
        }

        private void Count()
        {
            int i = 0;
            int tong = 0;
            try
            {
                DataView dv = dgrDSach_KHang.ItemsSource as DataView;
                DataRow[] drselect = dv.Table.Select("CHON is not null");
                foreach (DataRow dr in drselect)
                {
                    tong = tong + 1;
                    string t = dr["CHON"].ToString();
                    if (dr["CHON"].ToString() == "True")
                    {
                        i = i + 1;
                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            lblResult.Content = "Tổng số điểm đo: " + tong.ToString();
            
        }

        private void dtpNgayHLUC_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void btnLayDuLieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnGhi.IsEnabled = false;
                dtDDO.Clear();
                dtKHANG.Clear();
                dtVITRI.Clear();

                if (rdbSogcs.IsChecked == true)
                {
                    dgrDSach_KHang.Columns[7].Visibility = Visibility.Visible;
                    strMasogcs = txtMasogcs.Text.Trim().ToString();
                    dsAll = GlobalServices.Service_NPCUS.Get_THONGTIN_KHANG_sogcs(strMa_dviqly, strMasogcs, ref strErr);
                }
                else
                {
                    dgrDSach_KHang.Columns[7].Visibility = Visibility.Hidden;
                    strMatram = txtMasogcs.Text.Trim().ToString();
                    dsAll = GlobalServices.Service_NPCUS.Get_THONGTIN_KHANG_tram(strMa_dviqly, strMatram, ref strErr);
                }                

                if (dsAll != null && dsAll.Tables["KQ"].Rows.Count > 0)
                {
                    DataView dv = dsAll.Tables["KQ"].DefaultView;
                    dgrDSach_KHang.ItemsSource = dv;
                    dgrDSach_KHang.Columns[1].IsReadOnly = true;
                    dgrDSach_KHang.Columns[2].IsReadOnly = true;
                    //dgrDSach_KHang.Columns[3].IsReadOnly = true;
                    dgrDSach_KHang.Columns[4].IsReadOnly = true;
                    //dgrDSach_KHang.Columns[5].IsReadOnly = true;
                    dgrDSach_KHang.Columns[6].IsReadOnly = true;

                    dtDDO = dsAll.Tables["DIEMDO"].Copy();
                    dtKHANG = dsAll.Tables["KHANG"].Copy();
                    dtVITRI = dsAll.Tables["VITRI"].Copy();

                    btnGhi.IsEnabled = true;
                    Count();
                }
                else
                {
                    dgrDSach_KHang.ItemsSource = null;
                    btnGhi.IsEnabled = false;
                    lblResult.Content = "";
                    MessageBox.Show("Không có dữ liệu !");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dgrDSach_KHang_CurrentCellChanged(object sender, EventArgs e)
        {

        }

        private void chkChonTatCa_Click(object sender, RoutedEventArgs e)
        {

        }

        private void chkDaoChon_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnLayDuLieu1_Click(object sender, RoutedEventArgs e)
        {

        }

        private void txtNgayGCS_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void btnChon_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dgrDSach_SGCS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!dtpNgayHLUC.SelectedDate.HasValue)
                {
                    MessageBox.Show("Hãy chọn ngày hiệu lực !");
                    return;
                }

                if (MessageBox.Show("Bạn có chắc chắn ngày hiệu lực là: " + dtpNgayHLUC.SelectedDate.Value.Date.ToString("dd/MM/yyyy") + " không?", "Thông báo", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            

                if (checkchungDiachiKH == true)
                {
                    if (txtDiachiKH.Text.Trim().ToString() == string.Empty)
                    {
                        MessageBox.Show("Nhập địa chỉ khách hàng !");
                        txtDiachiKH.Focus();
                        return;
                    }
                    else
                    {
                        strDuongPho = txtDiachiKH.Text.Trim().ToString();
                    }

                    if (checkReAll == false) // thay thanh phan
                    {
                        if (txtReKH.Text.Trim().ToString() == string.Empty)
                        {
                            MessageBox.Show("Nhập chuỗi thay thế địa chỉ khách hàng !");
                            txtReKH.Focus();
                            return;
                        }
                        else
                        {
 
                        }
                    }

                }

                if (checkchungDiachiDDO == true)
                {
                    if (txtDiachiDDO.Text.Trim().ToString() == string.Empty)
                    {
                        MessageBox.Show("Nhập địa chỉ điểm đo !");
                        txtDiachiDDO.Focus();
                        return;
                    }
                    else
                    {
                        strDiachiDDO = txtDiachiDDO.Text.Trim().ToString();
                    }

                    if (checkReAll == false) // thay thanh phan
                    {
                        if (txtReDDO.Text.Trim().ToString() == string.Empty)
                        {
                            MessageBox.Show("Nhập chuỗi thay thế địa chỉ điểm đo !");
                            txtReDDO.Focus();
                            return;
                        }
                        else
                        {

                        }
                    }
                }

                if (dsAll != null)
                {
                    DataTable dt1 = dsAll.Tables[0] as DataTable;
                    
                    if (dt1.Select("CHON = 'true'").Count() < 1)
                    {
                        MessageBox.Show("Hãy chọn điểm đo cần thay đổi thông tin !");
                        this.Cursor = null;
                        return;
                    }
                    DataTable dt = dt1.Select("CHON = 'true'").CopyToDataTable();
                    dt.TableName = "KQ";

                    string strMakhangSS = "";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow drAll = dt.Rows[i];
                        strMA_KHANG = drAll["MA_KHANG"].ToString();
                        strMA_DDO = drAll["MA_DDO"].ToString();

                        DataRow drKH = dtKHANG.Select("MA_KHANG = '" + strMA_KHANG + "'")[0];
                        DataRow drDDO = dtDDO.Select("MA_DDO = '" + strMA_DDO + "'")[0];
                        DataRow drVITRI = dtVITRI.Select("MA_DDO = '" + strMA_DDO + "'")[0];

                        if (strMakhangSS != drKH["MA_KHANG"].ToString())
                        {
                            #region insert bảng HDG_KHACH_HANG
                            strMakhangSS = drKH["MA_KHANG"].ToString();
                            DsTableInsert.Tables["HDG_KHACH_HANG"].Rows.Clear();
                            DataRow drHDG_KHACH_HANG = DsTableInsert.Tables["HDG_KHACH_HANG"].NewRow();
                            drHDG_KHACH_HANG["MA_DVIQLY"] = strMa_dviqly;                            
                            drHDG_KHACH_HANG["MA_KHANG"] = drKH["MA_KHANG"];
                            drHDG_KHACH_HANG["SO_NHA"] = drKH["SO_NHA"];
                            if (checkchungDiachiKH == true)
                            {
                                if (checkReAll == true)
                                {
                                    drHDG_KHACH_HANG["DUONG_PHO"] = strDuongPho;
                                }
                                else
                                {
                                    string strgoc = drKH["DUONG_PHO"].ToString();
                                    string strRe = txtReKH.Text.Trim().ToString();
                                    drHDG_KHACH_HANG["DUONG_PHO"] = strgoc.Replace(strDuongPho, strRe);
                                }
                            }
                            else
                            {
                                drHDG_KHACH_HANG["DUONG_PHO"] = drAll["DUONG_PHO"];
                            }
                            drHDG_KHACH_HANG["DCHI_HDON"] = drKH["DCHI_HDON"];
                            drHDG_KHACH_HANG["ID_HDONG"] = drKH["ID_HDONG"];
                            drHDG_KHACH_HANG["MASO_THUE"] = drKH["MASO_THUE"];
                            drHDG_KHACH_HANG["TLE_THUE"] = drKH["TLE_THUE"];
                            drHDG_KHACH_HANG["LOAI_KHANG"] = drKH["LOAI_KHANG"];
                            drHDG_KHACH_HANG["MANHOM_KHANG"] = drKH["MANHOM_KHANG"];
                            drHDG_KHACH_HANG["MA_NN"] = drKH["MA_NN"];
                            drHDG_KHACH_HANG["TEN_KHANG"] = drKH["TEN_KHANG"];
                            drHDG_KHACH_HANG["TENHDON_KHANG"] = drKH["TENHDON_KHANG"];
                            drHDG_KHACH_HANG["TENTAT_KHANG"] = drKH["TENTAT_KHANG"];
                            drHDG_KHACH_HANG["NGAY_SINH"] = drKH["NGAY_SINH"];
                            drHDG_KHACH_HANG["NGAY_HLUC"] = Convert.ToDateTime(dtpNgayHLUC.SelectedDate.Value.ToString("MM/dd/yyyy"));
                            drHDG_KHACH_HANG["TRANG_THAI"] = 1;
                            drHDG_KHACH_HANG["THANG"] = dtpNgayHLUC.SelectedDate.Value.Month;
                            drHDG_KHACH_HANG["NAM"] = dtpNgayHLUC.SelectedDate.Value.Year;
                            drHDG_KHACH_HANG["MA_DVIDCHINH"] = drKH["MA_DVIDCHINH"];
                            drHDG_KHACH_HANG["TKHOAN_KHANG"] = drKH["TKHOAN_KHANG"];
                            drHDG_KHACH_HANG["MA_NHANG"] = drKH["MA_NHANG"];
                            drHDG_KHACH_HANG["MA_KHTT"] = drKH["MA_KHTT"];
                            drHDG_KHACH_HANG["MA_LOAIDN"] = drKH["MA_LOAIDN"];
                            drHDG_KHACH_HANG["DTHOAI"] = drAll["DTHOAI"];
                            drHDG_KHACH_HANG["FAX"] = drKH["FAX"];
                            drHDG_KHACH_HANG["EMAIL"] = drAll["EMAIL"];
                            drHDG_KHACH_HANG["WEBSITE"] = drKH["WEBSITE"];
                            drHDG_KHACH_HANG["GIOI_TINH"] = drKH["GIOI_TINH"];
                            drHDG_KHACH_HANG["MA_CNANG"] = "-1";
                            drHDG_KHACH_HANG["NGAY_TAO"] = DateTime.Now;
                            drHDG_KHACH_HANG["NGUOI_TAO"] = strTenDNHap + "_EDIT";
                            drHDG_KHACH_HANG["NGAY_SUA"] = DateTime.Now;
                            drHDG_KHACH_HANG["NGUOI_SUA"] = strTenDNHap + "_EDIT";
                            DsTableInsert.Tables["HDG_KHACH_HANG"].Rows.Add(drHDG_KHACH_HANG);
                            DsTableInsert.AcceptChanges();
                            if (!GlobalServices.Service_NPCUS.insert_HDG_KHACH_HANG_TDTT(DsTableInsert, ref strErr))
                            {
                                if (strErr.Contains("violated"))
                                {
                                    MessageBox.Show("Ngày hiệu lực phải lớn hơn ngày " + dtpNgayHLUC.SelectedDate.Value.Date.ToString("dd/MM/yyyy"));
                                }
                                else
                                {
                                    MessageBox.Show("Insert HDG_KHACH_HANG Failed: " + drKH["MA_KHANG"].ToString() + "\n" + strErr);
                                }
                                del_data_Inserted();
                                return;
                            }
                            // lưu lại thông tin để xóa trong trường hợp Insert không được
                            DataRow drHDG_KHACH_HANG_del = DsTableDelete.Tables["HDG_KHACH_HANG"].NewRow();
                            drHDG_KHACH_HANG_del["MA_DVIQLY"] = strMa_dviqly;
                            drHDG_KHACH_HANG_del["MA_KHANG"] = drKH["MA_KHANG"];
                            drHDG_KHACH_HANG_del["NGAY_HLUC"] = Convert.ToDateTime(dtpNgayHLUC.SelectedDate.Value.ToString("MM/dd/yyyy"));
                            drHDG_KHACH_HANG_del["TRANG_THAI"] = 1;
                            DsTableDelete.Tables["HDG_KHACH_HANG"].Rows.Add(drHDG_KHACH_HANG_del);
                            DsTableDelete.AcceptChanges();


                            #endregion
                        }

                        #region insert bảng HDG_DIEM_DO
                        DsTableInsert.Tables["HDG_DIEM_DO"].Rows.Clear();
                        DataRow drHDG_DIEM_DO = DsTableInsert.Tables["HDG_DIEM_DO"].NewRow();
                        drHDG_DIEM_DO["MA_DVIQLY"] = strMa_dviqly;
                        drHDG_DIEM_DO["MA_DDO"] = drDDO["MA_DDO"];
                        drHDG_DIEM_DO["ID_HDONG"] = drDDO["ID_HDONG"];
                        drHDG_DIEM_DO["MA_KHANG"] = drDDO["MA_KHANG"];
                        drHDG_DIEM_DO["MA_DDO_DDIEN"] = drDDO["MA_DDO_DDIEN"];
                        drHDG_DIEM_DO["MA_CAPDA"] = drDDO["MA_CAPDA"];
                        if (checkchungDiachiDDO == true)
                        {
                            if (checkReAll == true)
                            {
                                drHDG_DIEM_DO["DIA_CHI"] = strDiachiDDO;
                            }
                            else
                            {
                                string strgoc = drDDO["DIA_CHI"].ToString();
                                string strRe = txtReDDO.Text.Trim().ToString();
                                drHDG_DIEM_DO["DIA_CHI"] = strgoc.Replace(strDiachiDDO, strRe);
                            }
                        }
                        else
                        {
                            drHDG_DIEM_DO["DIA_CHI"] = drAll["DIA_CHI"];
                        }

                        drHDG_DIEM_DO["ID_DIA_CHINH"] = drDDO["ID_DIA_CHINH"];
                        drHDG_DIEM_DO["KIMUA_CSPK"] = drDDO["KIMUA_CSPK"];
                        drHDG_DIEM_DO["CSUAT"] = drDDO["CSUAT"];
                        drHDG_DIEM_DO["MUC_DICH"] = drDDO["MUC_DICH"];
                        drHDG_DIEM_DO["SO_PHA"] = drDDO["SO_PHA"];
                        drHDG_DIEM_DO["SOHUU_LUOI"] = drDDO["SOHUU_LUOI"];
                        drHDG_DIEM_DO["LOAI_DDO"] = drDDO["LOAI_DDO"];
                        drHDG_DIEM_DO["SO_HO"] = drDDO["SO_HO"];
                        drHDG_DIEM_DO["LAN_CAPNHAT"] = 1;
                        drHDG_DIEM_DO["THAO_TACDL"] = "THAYDOI_TT";
                        drHDG_DIEM_DO["NGAY_HLUC"] = Convert.ToDateTime(dtpNgayHLUC.SelectedDate.Value.ToString("MM/dd/yyyy"));
                        drHDG_DIEM_DO["MO_TA"] = drDDO["MO_TA"];
                        drHDG_DIEM_DO["TRANG_THAI"] = 1;
                        drHDG_DIEM_DO["TTRANG_TREOTHAO"] = 1;
                        drHDG_DIEM_DO["MA_CNANG"] = drDDO["MA_CNANG"];
                        drHDG_DIEM_DO["NGAY_TAO"] = DateTime.Now;
                        drHDG_DIEM_DO["NGUOI_TAO"] = strTenDNHap + "_EDIT";
                        drHDG_DIEM_DO["NGAY_SUA"] = DateTime.Now;
                        drHDG_DIEM_DO["NGUOI_SUA"] = strTenDNHap + "_EDIT";
                        DsTableInsert.Tables["HDG_DIEM_DO"].Rows.Add(drHDG_DIEM_DO);
                        DsTableInsert.AcceptChanges();
                        if (!GlobalServices.Service_NPCUS.insert_HDG_DIEM_DO_TDTT(DsTableInsert, ref strErr))
                        {
                            if (strErr.Contains("violated"))
                            {
                                MessageBox.Show("Ngày hiệu lực phải lớn hơn ngày " + dtpNgayHLUC.SelectedDate.Value.Date.ToString("dd/MM/yyyy"));
                            }
                            else
                            {
                                MessageBox.Show("Insert HDG_DIEM_DO Failed: " + drDDO["MA_DDO"].ToString() + "\n" + strErr);
                            }
                            del_data_Inserted();
                            return;
                        }

                        DataRow drHDG_DIEM_DO_del = DsTableDelete.Tables["HDG_DIEM_DO"].NewRow();
                        drHDG_DIEM_DO_del["MA_DVIQLY"] = strMa_dviqly;
                        drHDG_DIEM_DO_del["MA_DDO"] = drDDO["MA_DDO"];
                        drHDG_DIEM_DO_del["LAN_CAPNHAT"] = 1;
                        drHDG_DIEM_DO_del["NGAY_HLUC"] = Convert.ToDateTime(dtpNgayHLUC.SelectedDate.Value.ToString("MM/dd/yyyy"));
                        DsTableDelete.Tables["HDG_DIEM_DO"].Rows.Add(drHDG_DIEM_DO_del);
                        DsTableDelete.AcceptChanges();

                        #endregion

                        #region insert bảng HDG_VITRI_DDO
                        DsTableInsert.Tables["HDG_VITRI_DDO"].Rows.Clear();
                        DataRow drHDG_VITRI_DDO = DsTableInsert.Tables["HDG_VITRI_DDO"].NewRow();
                        drHDG_VITRI_DDO["MA_DVIQLY"] = strMa_dviqly;
                        //drHDG_VITRI_DDO["ID_VITRI_DDO"] = ma_khang + "001";
                        drHDG_VITRI_DDO["NGAY_HLUC"] = Convert.ToDateTime(dtpNgayHLUC.SelectedDate.Value.ToString("MM/dd/yyyy"));
                        drHDG_VITRI_DDO["MA_DDO"] = drVITRI["MA_DDO"];
                        drHDG_VITRI_DDO["MA_TRAM"] = drVITRI["MA_TRAM"];
                        drHDG_VITRI_DDO["MA_LO"] = drAll["MA_LO"];
                        drHDG_VITRI_DDO["MA_TO"] = drAll["MA_TO"];
                        drHDG_VITRI_DDO["PHA"] = drVITRI["PHA"];
                        drHDG_VITRI_DDO["SO_COT"] = drAll["SO_COT"];
                        drHDG_VITRI_DDO["SO_HOM"] = drAll["SO_HOM"];
                        drHDG_VITRI_DDO["MA_CNANG"] = "-1";
                        drHDG_VITRI_DDO["NGAY_TAO"] = DateTime.Now;
                        drHDG_VITRI_DDO["NGUOI_TAO"] = strTenDNHap + "_EDIT";
                        drHDG_VITRI_DDO["NGAY_SUA"] = DateTime.Now;
                        drHDG_VITRI_DDO["NGUOI_SUA"] = strTenDNHap + "_EDIT";
                        DsTableInsert.Tables["HDG_VITRI_DDO"].Rows.Add(drHDG_VITRI_DDO);
                        DsTableInsert.AcceptChanges();
                        if (!GlobalServices.Service_NPCUS.insert_HDG_VITRI_DDO_TDTT(DsTableInsert, ref id_vitri_ddo, ref strErr))
                        {
                            if (strErr.Contains("violated"))
                            {
                                MessageBox.Show("Ngày hiệu lực phải lớn hơn ngày " + dtpNgayHLUC.SelectedDate.Value.Date.ToString("dd/MM/yyyy"));
                            }
                            else
                            {
                                MessageBox.Show("Insert HDG_VITRI_DDO Failed: " + drVITRI["MA_DDO"].ToString() + "\n" + strErr);
                            }
                            del_data_Inserted();
                            return;
                        }

                        DataRow drHDG_VITRI_DDO_del = DsTableDelete.Tables["HDG_VITRI_DDO"].NewRow();
                        drHDG_VITRI_DDO_del["MA_DVIQLY"] = strMa_dviqly;
                        drHDG_VITRI_DDO_del["ID_VITRI_DDO"] = id_vitri_ddo;
                        DsTableDelete.Tables["HDG_VITRI_DDO"].Rows.Add(drHDG_VITRI_DDO_del);
                        DsTableDelete.AcceptChanges();

                        #endregion
                        
                    }

                    MessageBox.Show("Cập nhật thông tin thành công !");

                }
                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ckbDiachiKH_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                txtDiachiKH.IsEnabled = true;
                checkchungDiachiKH = true;
                if (checkReAll == true)
                {
                }
                else
                {
                    txtReKH.IsEnabled = true;
                }
                //dgrDSach_KHang.Columns[3].IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ckbDiachiKH_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                txtDiachiKH.IsEnabled = false;
                txtDiachiKH.Text = "";
                checkchungDiachiKH = false;                
                txtReKH.IsEnabled = false;
                
                //dgrDSach_KHang.Columns[3].IsReadOnly = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void chkChonTatCa_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView dv = dgrDSach_KHang.ItemsSource as DataView;
                DataRow[] drselect = dv.Table.Select("CHON=0");
                foreach (DataRow dr in drselect)
                {
                    dr["CHON"] = 1;
                }
                dgrDSach_KHang.ItemsSource = dv.Table.DefaultView;
                dv.Table.DefaultView.AllowNew = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            Count();
        }

        private void chkDaoChon_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView dv = dgrDSach_KHang.ItemsSource as DataView;
                DataRow[] drselect = dv.Table.Select("CHON is not null");
                foreach (DataRow dr in drselect)
                {
                    if (dr["CHON"].ToString() == "True")
                    {
                        dr["CHON"] = 0;
                    }

                    else
                    {
                        dr["CHON"] = 1;
                    }
                }

                dgrDSach_KHang.ItemsSource = dv.Table.DefaultView;
                dv.Table.DefaultView.AllowNew = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            Count();
        }

        private void chkDaoChon_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView dv = dgrDSach_KHang.ItemsSource as DataView;
                DataRow[] drselect = dv.Table.Select("CHON is not null");
                foreach (DataRow dr in drselect)
                {
                    string t = dr["CHON"].ToString();
                    if (dr["CHON"].ToString() == "True")
                    {
                        dr["CHON"] = 0;
                    }
                    else
                    {
                        dr["CHON"] = 1;
                    }
                }

                dgrDSach_KHang.ItemsSource = dv.Table.DefaultView;
                dv.Table.DefaultView.AllowNew = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            Count();
        }

        private void ckbDiachiDDO_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                txtDiachiDDO.IsEnabled = true;
                checkchungDiachiDDO = true;
                if (checkReAll == true)
                {
                }
                else
                {
                    txtReDDO.IsEnabled = true;
                }
                //dgrDSach_KHang.Columns[5].IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ckbDiachiDDO_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                txtDiachiDDO.IsEnabled = false;
                txtDiachiDDO.Text = "";
                checkchungDiachiDDO = false;                
                txtReDDO.IsEnabled = false;
                
                //dgrDSach_KHang.Columns[5].IsReadOnly = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void rdbAll_Checked(object sender, RoutedEventArgs e)
        {
            
            checkReAll = true;

            if (checkchungDiachiKH == true)
            {
                txtDiachiKH.IsEnabled = true; 
                txtReKH.IsEnabled = false;
            }
            else
            {
                txtDiachiKH.IsEnabled = false;
                txtReKH.IsEnabled = false;
            }

            if (checkchungDiachiDDO == true)
            {
                txtDiachiDDO.IsEnabled = true;
                txtReDDO.IsEnabled = false;
            }
            else
            {
                txtDiachiDDO.IsEnabled = false;
                txtReDDO.IsEnabled = false;
            }            
        }

        private void rdbnotAll_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                checkReAll = false;

                if (checkchungDiachiKH == true)
                {
                    txtDiachiKH.IsEnabled = true;
                    txtReKH.IsEnabled = true;
                }
                else
                {
                    txtDiachiKH.IsEnabled = false;
                    txtReKH.IsEnabled = false;
                }

                if (checkchungDiachiDDO == true)
                {
                    txtDiachiDDO.IsEnabled = true;
                    txtReDDO.IsEnabled = true;
                }
                else
                {
                    txtDiachiDDO.IsEnabled = false;
                    txtReDDO.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void rdbAll_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void rdbnotAll_Unchecked(object sender, RoutedEventArgs e)
        {

        }
    }
}
